package com.tupi.project.di.module;


import com.tupi.project.BuildConfig;
import com.tupi.project.base.App;
import com.tupi.project.component.Constants;
import com.tupi.project.http.ApiUrl;
import com.tupi.project.http.HttpApis;
import com.tupi.project.http.component.AddCookiesInterceptor;
import com.tupi.project.http.component.FileResponseBody;
import com.tupi.project.http.component.LoggerInterceptor;
import com.tupi.project.http.component.SaveCookiesInterceptor;
import com.tupi.project.util.SystemUtil;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by codeest on 2017/2/26.
 */

@Module
public class HttpModule
{
    
    @Singleton
    @Provides
    Retrofit.Builder provideRetrofitBuilder()
    {
        return new Retrofit.Builder();
    }
    
    
    @Singleton
    @Provides
    OkHttpClient.Builder provideOkHttpBuilder()
    {
        return new OkHttpClient.Builder();
    }
    
    @Singleton
    @Provides
    OkHttpClient provideClient(OkHttpClient.Builder builder)
    {
        if (BuildConfig.DEBUG)
        {
            builder.addInterceptor(new LoggerInterceptor("httpTag",true));
//            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
//            builder.addInterceptor(loggingInterceptor);
        }
        File cacheFile = new File(Constants.PATH_CACHE);
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
        Interceptor cacheInterceptor = new Interceptor()
        {
            @Override
            public Response intercept(Chain chain) throws IOException
            {
                Request request = chain.request();
                if (!SystemUtil.isNetworkConnected())
                {
                    request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
                }
                Response response = chain.proceed(request);
                if (SystemUtil.isNetworkConnected())
                {
                    int maxAge = 0;
                    // 有网络时, 不缓存, 最大保存时长为0
                    response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge)
                            .removeHeader("Pragma").build();
                } else
                {
                    // 无网络时，设置超时为4周
                    int maxStale = 60 * 60 * 24 * 28;
                    response.newBuilder().header("Cache-Control", "public, only-if-cached, " +
                            "max-stale=" + maxStale).removeHeader("Pragma").build();
                }
                return response;
            }
        };
//        Interceptor apikey = new Interceptor() {
//            @Override
//            public Response intercept(Chain chain) throws IOException {
//                Request request = chain.request();
//                request = request.newBuilder()
//                        .addHeader("apikey",Constants.KEY_API)
//                        .build();
//                return chain.proceed(request);
//            }
//        }
//        设置统一的请求头部参数
//        builder.addInterceptor(apikey);
        //设置缓存
        builder.addNetworkInterceptor(cacheInterceptor);
        builder.addInterceptor(cacheInterceptor);
        builder.addInterceptor(new AddCookiesInterceptor(App.getInstance()));
        builder.addInterceptor(new SaveCookiesInterceptor(App.getInstance()));
        builder.addInterceptor(new Interceptor()
        {
            @Override
            public Response intercept(Chain chain) throws IOException
            {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder().body(new FileResponseBody(originalResponse))
                        .build();
            }
        });
        builder.cache(cache);
        //设置超时
        builder.connectTimeout(10, TimeUnit.SECONDS);
        builder.readTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(20, TimeUnit.SECONDS);
        //错误重连
        builder.retryOnConnectionFailure(true);
        return builder.build();
    }
    
    @Singleton
    @Provides
    Retrofit provideRetrofit(Retrofit.Builder builder, OkHttpClient client)
    {
        return builder.baseUrl(ApiUrl.SERVER_IP).client(client).addCallAdapterFactory
                (RxJava2CallAdapterFactory.create()).addConverterFactory(GsonConverterFactory
                .create()).build();
    }
    
    @Singleton
    @Provides
    HttpApis provideHttpApis(Retrofit retrofit)
    {
        return retrofit.create(HttpApis.class);
    }
    
}
